#include <stdio.h>
#include <stdlib.h>


#include "nohead.h"
/* 首部插入*/
int list_insert(struct node_st **list,struct score_st *data)
{
	struct node_st *new;

	new = malloc(sizeof(*new));
	if(new == NULL)
		return -1;


	new->data = *data;
	/*new->next = NULL;*/

	new->next = *list;
	*list = new;/* 当成一般模型处理 千万别当成最简模型*/

	return 0;
}
/* 遍历无头结点的链表*/
void list_show(struct node_st *list)
{
	struct node_st *temp = list;

	while(temp)
	{
		printf("%d %s %d %d\n",temp->data.id,temp->data.name,temp->data.math,temp->data.chinese);
		temp = temp->next;
	}
}

/* 删除第一个结点*/
int list_delete(struct node_st **list)
{
	if (*list == NULL)
		return -1;
	struct node_st *temp = *list;
	*list = (*list)->next;/* 第二个结点*/
	free(temp);
	
	return 0;
}

struct score_st * list_find(struct node_st *list,int id)
{
	if (id < 0)
		return NULL;
	struct node_st *temp = list;
#if 0
	while(temp->data.id != id && temp)
	{	
		temp = temp->next;
	}
	
	if(temp)
		return 0;
	else
		return -2;
#endif/*传入id不存在会段错误*/

	for (temp = list;temp;temp = temp->next)
	{
		if(temp->data.id == id)
			return &temp->data;
	}

	return NULL;
}


/*单向不循环无头结点链表无需创建*/


void list_destroy(struct node_st *list)
{
	struct node_st *temp = list,*temp2;
	if (temp)
	{
		while(temp)
		{
			temp2 = temp->next;
			free(temp);
			temp  = temp2;
		}
	}
	else
	{
		return ;
	}
}
